package com.fy56.platform.logistics.restful.api.controller.wx;


import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.model.ReceiverContactInfo;
import com.fy56.platform.logistics.model.SenderContactInfo;
import com.fy56.platform.logistics.model.Vip;
import com.fy56.platform.logistics.restful.api.app.authorization.annotation.Authorization;
import com.fy56.platform.logistics.service.wx.IReceiverContactInfoService;
import com.fy56.platform.logistics.service.wx.ISenderContactInfoService;
import com.fy56.platform.logistics.service.wx.IVipService;
import com.fy56.platform.logistics.utils.IdWorkerTools;
import com.google.common.collect.Maps;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 发货联系人RestController
 * Created by li on 2018-05-29.
 */
@Api(value = "SenderContactInfoManager", description = "发货联系人管理", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/senderContactInfo")
public class SenderContactInfoController {

    @Autowired
    private IVipService vipService;
    @Autowired
    private ISenderContactInfoService iSenderContactInfoService;
    @Autowired
    private IReceiverContactInfoService iReceiverContactInfoService;

    private static Logger log = Logger.getLogger(SenderContactInfoController.class);

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ApiOperation(value = "查询发货联系人")
    @Authorization
    public ResponseEntity search(@RequestParam String vipMemberId) {
        try {
            return new ResponseEntity(ResultModel.ok(iSenderContactInfoService.selectByVipMemberId(Long.valueOf(vipMemberId))), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询发货联系人失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "查询发货联系人失败"), HttpStatus.OK);
        }
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加发货联系人")
    @Authorization
    public ResponseEntity add(@RequestParam String data) {
        SenderContactInfo model = null;
        try {
            model = JSON.parseObject(data).toJavaObject(SenderContactInfo.class);

            //验证发货联系人是否存在，若发货联系人已存在，提示：该发货联系人已经存在,请重新输入
            if(iSenderContactInfoService.getExistsByVipMemberIdAndMobile(model) != null){
                return new ResponseEntity(ResultModel.error("发货联系人[" + model.getContactName() + " "
                        + model.getContactMobile() + "]已存在"), HttpStatus.OK);
            }

            //执行插入并返回
            model.setCreateTime(new Date());
            return new ResponseEntity(ResultModel.ok(iSenderContactInfoService.insert(model)), HttpStatus.OK);

        } catch (Exception e) {
            log.error("添加发货联系人失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "添加发货联系人失败"), HttpStatus.OK);
        } finally {
            model = null;
        }
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "修改发货联系人")
    @Authorization
    public ResponseEntity edit(@RequestParam String data) {
        SenderContactInfo model = null;
        try {
            model = JSON.parseObject(data).toJavaObject(SenderContactInfo.class);
            model.setUpdateTime(new Date());
            return new ResponseEntity(ResultModel.ok(iSenderContactInfoService.update(model)), HttpStatus.OK);
        } catch (Exception e) {
            log.error("修改发货联系人失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "修改发货联系人失败"), HttpStatus.OK);
        } finally {
            model = null;
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除发货联系人")
    @Authorization
    public ResponseEntity delete(@RequestParam String id) {
        try {
            return new ResponseEntity(ResultModel.ok(iSenderContactInfoService.delete(Long.valueOf(id))), HttpStatus.OK);
        } catch (Exception e) {
            log.error("删除发货联系人失败",e);
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "删除发货联系人失败"), HttpStatus.OK);
        }
    }

    @RequestMapping(value = "/getDefaultReceiverSenderByOpenId", method = RequestMethod.POST)
    @ApiOperation(value = "根据用户ID，查询默认收发货人")
    @Authorization
    public ResponseEntity getDefaultReceiverSenderByOpenId(@RequestParam String openId) {
        ResultModel resultModel = null;
        try {
            Map<String, Object> resultMap = null;
            Vip user =  vipService.selectByOpenId(openId);
            if(user != null) {
                resultMap = iSenderContactInfoService.getDefaultReceiverSenderByUserIdService(user.getId());
            }
            resultModel = new ResultModel(100, "success", resultMap);
        } catch (Exception e) {
            log.error("根据用户ID 查询默认收发货人信息 功能异常: ", e);
            resultModel = new ResultModel(101,"根据用户ID 查询默认收发货人信息 功能异常: " + e.getMessage(), null);
        }
        return new ResponseEntity(resultModel, HttpStatus.OK);
    }

    @RequestMapping(value = "/getDefaultReceiverByList", method = RequestMethod.POST)
    @ApiOperation(value = "根据用户ID，查询默认收货人")
    @Authorization
    public ResponseEntity getDefaultReceiverByList(@RequestParam String openId, @RequestParam String contactMobile,@RequestParam  String contactName) {
        ResultModel resultModel = null;
        Map<String, Object> resultMap = Maps.newHashMap();
        try {
            Vip user =  vipService.selectByOpenId(openId);
            if(user != null) {
                ReceiverContactInfo receiverContactInfo = new ReceiverContactInfo();
                receiverContactInfo.setContactMobile(contactMobile);
                receiverContactInfo.setContactName(contactName);
                List<ReceiverContactInfo> list = iReceiverContactInfoService.selectByVipMemberIdandName(receiverContactInfo);
                if (list!=null&&list.size()>0){
                    // 默认收货人
                    resultMap.put("defaultReceiver",list.get(0));
                }
            }
            resultModel = new ResultModel(100, "success", resultMap);
        } catch (Exception e) {
            log.error("根据用户ID 查询默认收发货人信息 功能异常: ", e);
            resultModel = new ResultModel(101,"根据用户ID 查询默认收发货人信息 功能异常: " + e.getMessage(), null);
        }
        return new ResponseEntity(resultModel, HttpStatus.OK);
    }

}